package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DzCodeMapper;
import com.ruoyi.system.domain.DzCode;
import com.ruoyi.system.service.IDzCodeService;


import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.mapper.DzHuiyuanMapper;
import com.ruoyi.system.domain.DzHuiyuan;
import com.ruoyi.system.service.IDzHuiyuanService;

/**
 * 验证码Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-10-12
 */
@Service
public class DzCodeServiceImpl implements IDzCodeService 
{
    @Autowired
    private DzCodeMapper dzCodeMapper;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private IDzHuiyuanService dzHuiyuanService;

    /**
     * 查询验证码
     * 
     * @param codeId 验证码主键
     * @return 验证码
     */
    @Override
    public DzCode selectDzCodeByCodeId(Long codeId)
    {
        return dzCodeMapper.selectDzCodeByCodeId(codeId);
    }

    /**
     * 查询验证码列表
     * 
     * @param dzCode 验证码
     * @return 验证码
     */
    @Override
    public List<DzCode> selectDzCodeList(DzCode dzCode)
    {

        //权限控制
        SysUser user=  SecurityUtils.getLoginUser().getUser();
        List<SysRole> listRole= sysRoleService.selectRolesByUserId(user.getUserId());
        int datascope = 5;
        for(SysRole role:listRole){
            if( role.getDataScope()!=null&&Integer.parseInt(role.getDataScope())<datascope){
                datascope=Integer.parseInt(role.getDataScope());
            }
        }
        dzCode.setDatascope(datascope);
        if(datascope==4){
            dzCode.setCheckDeptId(user.getDeptId());
        }
        if(datascope==5){
            DzHuiyuan huiyuan =  dzHuiyuanService.selectDzHuiyuanByDn(user.getPhonenumber());
            if(huiyuan!=null){
                dzCode.setCheckHuiyuanId(huiyuan.getId());
            }else{
                dzCode.setCheckHuiyuanId(0l);
            }

        }
        return dzCodeMapper.selectDzCodeList(dzCode);
    }

    @Override
    public List<DzCode> selectDzCodeListWeb(DzCode dzCode)
    {
        return dzCodeMapper.selectDzCodeList(dzCode);
    }

    /**
     * 新增验证码
     * 
     * @param dzCode 验证码
     * @return 结果
     */
    @Override
    public int insertDzCode(DzCode dzCode)
    {
        dzCode.setCreateTime(DateUtils.getNowDate());
        return dzCodeMapper.insertDzCode(dzCode);
    }

    /**
     * 修改验证码
     * 
     * @param dzCode 验证码
     * @return 结果
     */
    @Override
    public int updateDzCode(DzCode dzCode)
    {
        return dzCodeMapper.updateDzCode(dzCode);
    }

    /**
     * 批量删除验证码
     * 
     * @param codeIds 需要删除的验证码主键
     * @return 结果
     */
    @Override
    public int deleteDzCodeByCodeIds(Long[] codeIds)
    {
        return dzCodeMapper.deleteDzCodeByCodeIds(codeIds);
    }

    /**
     * 删除验证码信息
     * 
     * @param codeId 验证码主键
     * @return 结果
     */
    @Override
    public int deleteDzCodeByCodeId(Long codeId)
    {
        return dzCodeMapper.deleteDzCodeByCodeId(codeId);
    }
}
